package com.bianmaba.batch.example.db.batch;

import com.bianmaba.batch.example.db.bean.Log;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

/**
 * @program: spring-batch-examples
 * @description:
 * @author: Chenjiabin
 * @create: 2019/1/8 11:29
 **/
@Service
public class LogReader extends FlatFileItemReader<Log> {
    public LogReader() {
        super();
        this.setResource(new ClassPathResource("log.csv"));
        this.setEncoding("GBK");
        this.setLineMapper(new DefaultLineMapper<Log>() {
            {
                setLineTokenizer(new DelimitedLineTokenizer() {
                    {
                        setNames(new String[]{"name", "dt", "logins"});
                    }
                });
                setFieldSetMapper(new BeanWrapperFieldSetMapper<Log>() {{
                    setTargetType(Log.class);
                }});
            }
        });
    }
}
